using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public static class RectTransformUtil 
{
    public static Rect GetWorldRect(RectTransform rt)
    {
        var localRect = rt.rect;
        //Debug.Log("[GetWorkRect] localRect: " + localRect);
        var worldRect = SpaceUtil.TransformRect(rt, localRect);
        //Debug.Log("[GetWorkRect] worldRect: " + worldRect);
        return worldRect;
    }

    public static void SetByWorldRect(RectTransform rt, Rect workRect)
    {
        var localRect = SpaceUtil.InverseTransformRect(rt, workRect);
        rt.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, localRect.width);
        rt.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, localRect.height);

        var pivot = rt.pivot;
        var x = workRect.x + workRect.width * pivot.x;
        var y = workRect.y + workRect.height * pivot.y;
        rt.position = new Vector3(x, y, 0);
    }
}
